import java.util.Scanner;

public class MaxSub {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数组大小: ");
        int n = scanner.nextInt();
        int[] arr = new int[n];

        System.out.print("请输入数组元素: ");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        int maxSum = MaxSum(arr);
        System.out.println("最大子序列和为: " + maxSum);

        scanner.close();
    }

    public static int MaxSum(int[] a) {
        if (a == null || a.length == 0) { // 如果数组为空，直接返回0
            return 0;
        }

        int sum = 0;
        int max = a[0];

        for (int i = 0; i < a.length; i++) {
            sum = sum + a[i];
            if (sum < a[i]) {
                sum = a[i];
            }
            if (sum > max) {
                max = sum;
            }
        }

        return max;
    }
}